using System;
using System.Globalization;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using SharePoint.ImportExport.Core;
using SharePoint2003Import;
namespace SharePointImporter.UserInterface
{

	/// <summary>
	/// Summary description for SharePointImporter.
	/// </summary>
	public class ImportApplication : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TabControl Settings;
		private System.Windows.Forms.PictureBox pictFooter;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.TabPage tabPage1;
		private System.Windows.Forms.TabPage tabPage2;
		private System.Windows.Forms.Button btnNext1;
		private System.Windows.Forms.TextBox txtTarget;
		private System.Windows.Forms.TextBox txtSource;
		private System.Windows.Forms.CheckBox chkAllowImportIntoExistingSite;
		private System.Windows.Forms.CheckBox chkImportSecurity;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.CheckBox chkAllowImportIntoExistingLibrary;
		private System.Windows.Forms.RichTextBox rtb_EventLog;
		private System.Windows.Forms.Button btnClose;
		private System.Windows.Forms.Button btnStart;

		private static Importer ImportGlobals =null;
		private System.Windows.Forms.Button btnValidate;
		private System.Windows.Forms.OpenFileDialog openStructDialog;
		private System.Windows.Forms.Button btnFileOpen;
		private System.Windows.Forms.Label Source;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.RadioButton rdReportAll;
		private System.Windows.Forms.RadioButton rdReportWarnings;
		private System.Windows.Forms.RadioButton rdReportErrors;
		private System.Windows.Forms.ToolTip toolTip1;
		private System.Windows.Forms.CheckBox chkImportDesign;
		private System.Windows.Forms.CheckBox chkTryToUpdateExistingItems;
		private System.ComponentModel.IContainer components;

		/// <summary>
		/// Default constructor for the form
		/// </summary>
		public ImportApplication()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			try 
			{
				txtTarget.Text="http://" + System.Environment.MachineName;
				// Create an instance of StreamReader to read from a file.
				// The using statement also closes the StreamReader.
				using(StreamReader sr = new StreamReader("config.dat"))
					{
						String line=sr.ReadLine();
						txtSource.Text=line;
						line=sr.ReadLine();
						chkImportSecurity.Checked=bool.Parse(line);
						line=sr.ReadLine();
						chkAllowImportIntoExistingSite.Checked=bool.Parse(line);
						line=sr.ReadLine();
						chkAllowImportIntoExistingLibrary.Checked=bool.Parse(line);
						line=sr.ReadLine();
						rdReportAll.Checked=bool.Parse(line);
						line=sr.ReadLine();
						rdReportWarnings.Checked=bool.Parse(line);
						line=sr.ReadLine();
						rdReportErrors.Checked=bool.Parse(line);
						txtTarget.Text=sr.ReadLine();
						line=sr.ReadLine();
						chkImportDesign.Checked=bool.Parse(line);
			
					}
			}
			catch (ArgumentException argEx) 
			{
				Logger.Log(ErrorLevel.NoLog,"Error :"+argEx.Message);
			}
			catch (IOException e) 
			{
				// Let the user know what went wrong.
				Console.WriteLine("The file could not be read:");
				Console.WriteLine(e.Message);
			}
			catch (Exception ex) 
			{
				// Let the user know what went wrong.
				Console.WriteLine("Errors processing the config.dat file:");
				Console.WriteLine(ex.Message);
			}
			if(txtTarget.Text.Length==0)
				txtTarget.Text="http://" + System.Environment.MachineName;
			
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		static void UnhandledHandler(object sender, UnhandledExceptionEventArgs args) 
		{
			Exception e = (Exception) args.ExceptionObject;
			Console.WriteLine("UnhandledHandler caught : " + e.Message);

		}


		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ImportApplication));
			this.Settings = new System.Windows.Forms.TabControl();
			this.tabPage1 = new System.Windows.Forms.TabPage();
			this.chkImportDesign = new System.Windows.Forms.CheckBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.rdReportAll = new System.Windows.Forms.RadioButton();
			this.rdReportWarnings = new System.Windows.Forms.RadioButton();
			this.rdReportErrors = new System.Windows.Forms.RadioButton();
			this.btnFileOpen = new System.Windows.Forms.Button();
			this.txtTarget = new System.Windows.Forms.TextBox();
			this.txtSource = new System.Windows.Forms.TextBox();
			this.chkAllowImportIntoExistingSite = new System.Windows.Forms.CheckBox();
			this.chkImportSecurity = new System.Windows.Forms.CheckBox();
			this.label2 = new System.Windows.Forms.Label();
			this.Source = new System.Windows.Forms.Label();
			this.chkAllowImportIntoExistingLibrary = new System.Windows.Forms.CheckBox();
			this.btnNext1 = new System.Windows.Forms.Button();
			this.btnValidate = new System.Windows.Forms.Button();
			this.tabPage2 = new System.Windows.Forms.TabPage();
			this.btnClose = new System.Windows.Forms.Button();
			this.btnStart = new System.Windows.Forms.Button();
			this.rtb_EventLog = new System.Windows.Forms.RichTextBox();
			this.pictFooter = new System.Windows.Forms.PictureBox();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.openStructDialog = new System.Windows.Forms.OpenFileDialog();
			this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
			this.chkTryToUpdateExistingItems = new System.Windows.Forms.CheckBox();
			this.Settings.SuspendLayout();
			this.tabPage1.SuspendLayout();
			this.groupBox1.SuspendLayout();
			this.tabPage2.SuspendLayout();
			this.SuspendLayout();
			// 
			// Settings
			// 
			this.Settings.Controls.Add(this.tabPage1);
			this.Settings.Controls.Add(this.tabPage2);
			this.Settings.Location = new System.Drawing.Point(0, 72);
			this.Settings.Name = "Settings";
			this.Settings.SelectedIndex = 0;
			this.Settings.Size = new System.Drawing.Size(600, 408);
			this.Settings.TabIndex = 0;
			// 
			// tabPage1
			// 
			this.tabPage1.Controls.Add(this.chkImportDesign);
			this.tabPage1.Controls.Add(this.groupBox1);
			this.tabPage1.Controls.Add(this.btnFileOpen);
			this.tabPage1.Controls.Add(this.txtTarget);
			this.tabPage1.Controls.Add(this.txtSource);
			this.tabPage1.Controls.Add(this.chkAllowImportIntoExistingSite);
			this.tabPage1.Controls.Add(this.chkImportSecurity);
			this.tabPage1.Controls.Add(this.label2);
			this.tabPage1.Controls.Add(this.Source);
			this.tabPage1.Controls.Add(this.chkAllowImportIntoExistingLibrary);
			this.tabPage1.Controls.Add(this.btnNext1);
			this.tabPage1.Controls.Add(this.btnValidate);
			this.tabPage1.Controls.Add(this.chkTryToUpdateExistingItems);
			this.tabPage1.Location = new System.Drawing.Point(4, 22);
			this.tabPage1.Name = "tabPage1";
			this.tabPage1.Size = new System.Drawing.Size(592, 382);
			this.tabPage1.TabIndex = 0;
			this.tabPage1.Text = "Settings";
			// 
			// chkImportDesign
			// 
			this.chkImportDesign.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.chkImportDesign.Location = new System.Drawing.Point(16, 104);
			this.chkImportDesign.Name = "chkImportDesign";
			this.chkImportDesign.Size = new System.Drawing.Size(232, 24);
			this.chkImportDesign.TabIndex = 6;
			this.chkImportDesign.Text = "Import &Design";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.rdReportAll);
			this.groupBox1.Controls.Add(this.rdReportWarnings);
			this.groupBox1.Controls.Add(this.rdReportErrors);
			this.groupBox1.Location = new System.Drawing.Point(8, 208);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(464, 104);
			this.groupBox1.TabIndex = 10;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Reporting";
			// 
			// rdReportAll
			// 
			this.rdReportAll.Checked = true;
			this.rdReportAll.Location = new System.Drawing.Point(16, 24);
			this.rdReportAll.Name = "rdReportAll";
			this.rdReportAll.TabIndex = 0;
			this.rdReportAll.TabStop = true;
			this.rdReportAll.Text = "Report All ";
			// 
			// rdReportWarnings
			// 
			this.rdReportWarnings.Location = new System.Drawing.Point(16, 48);
			this.rdReportWarnings.Name = "rdReportWarnings";
			this.rdReportWarnings.Size = new System.Drawing.Size(192, 24);
			this.rdReportWarnings.TabIndex = 1;
			this.rdReportWarnings.Text = "Report Warnings and Errors";
			// 
			// rdReportErrors
			// 
			this.rdReportErrors.Location = new System.Drawing.Point(16, 72);
			this.rdReportErrors.Name = "rdReportErrors";
			this.rdReportErrors.TabIndex = 2;
			this.rdReportErrors.Text = "Report Errors";
			// 
			// btnFileOpen
			// 
			this.btnFileOpen.Location = new System.Drawing.Point(512, 16);
			this.btnFileOpen.Name = "btnFileOpen";
			this.btnFileOpen.Size = new System.Drawing.Size(16, 24);
			this.btnFileOpen.TabIndex = 2;
			this.btnFileOpen.Text = "+";
			this.btnFileOpen.Click += new System.EventHandler(this.btnFileOpen_Click);
			// 
			// txtTarget
			// 
			this.txtTarget.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.txtTarget.Location = new System.Drawing.Point(144, 40);
			this.txtTarget.Name = "txtTarget";
			this.txtTarget.Size = new System.Drawing.Size(360, 20);
			this.txtTarget.TabIndex = 4;
			this.txtTarget.Text = "";
			// 
			// txtSource
			// 
			this.txtSource.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.txtSource.Location = new System.Drawing.Point(144, 16);
			this.txtSource.Name = "txtSource";
			this.txtSource.Size = new System.Drawing.Size(360, 20);
			this.txtSource.TabIndex = 1;
			this.txtSource.Text = "";
			// 
			// chkAllowImportIntoExistingSite
			// 
			this.chkAllowImportIntoExistingSite.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.chkAllowImportIntoExistingSite.Location = new System.Drawing.Point(16, 128);
			this.chkAllowImportIntoExistingSite.Name = "chkAllowImportIntoExistingSite";
			this.chkAllowImportIntoExistingSite.Size = new System.Drawing.Size(320, 24);
			this.chkAllowImportIntoExistingSite.TabIndex = 8;
			this.chkAllowImportIntoExistingSite.Text = "&Allow import into existing sites/areas";
			// 
			// chkImportSecurity
			// 
			this.chkImportSecurity.Checked = true;
			this.chkImportSecurity.CheckState = System.Windows.Forms.CheckState.Checked;
			this.chkImportSecurity.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.chkImportSecurity.Location = new System.Drawing.Point(16, 80);
			this.chkImportSecurity.Name = "chkImportSecurity";
			this.chkImportSecurity.Size = new System.Drawing.Size(128, 24);
			this.chkImportSecurity.TabIndex = 5;
			this.chkImportSecurity.Text = "Import &Security";
			// 
			// label2
			// 
			this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label2.Location = new System.Drawing.Point(16, 40);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(120, 16);
			this.label2.TabIndex = 3;
			this.label2.Text = "Target:";
			// 
			// Source
			// 
			this.Source.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Source.Location = new System.Drawing.Point(16, 16);
			this.Source.Name = "Source";
			this.Source.Size = new System.Drawing.Size(112, 16);
			this.Source.TabIndex = 0;
			this.Source.Text = "Source directory:";
			// 
			// chkAllowImportIntoExistingLibrary
			// 
			this.chkAllowImportIntoExistingLibrary.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.chkAllowImportIntoExistingLibrary.Location = new System.Drawing.Point(16, 152);
			this.chkAllowImportIntoExistingLibrary.Name = "chkAllowImportIntoExistingLibrary";
			this.chkAllowImportIntoExistingLibrary.Size = new System.Drawing.Size(320, 24);
			this.chkAllowImportIntoExistingLibrary.TabIndex = 9;
			this.chkAllowImportIntoExistingLibrary.Text = "Allow import into existing &libraries";
			// 
			// btnNext1
			// 
			this.btnNext1.BackColor = System.Drawing.SystemColors.HotTrack;
			this.btnNext1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnNext1.ForeColor = System.Drawing.SystemColors.Menu;
			this.btnNext1.Location = new System.Drawing.Point(488, 352);
			this.btnNext1.Name = "btnNext1";
			this.btnNext1.Size = new System.Drawing.Size(96, 24);
			this.btnNext1.TabIndex = 12;
			this.btnNext1.Text = "&Next";
			this.btnNext1.Click += new System.EventHandler(this.btnNext1_Click);
			// 
			// btnValidate
			// 
			this.btnValidate.BackColor = System.Drawing.SystemColors.HotTrack;
			this.btnValidate.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnValidate.ForeColor = System.Drawing.SystemColors.Menu;
			this.btnValidate.Location = new System.Drawing.Point(8, 352);
			this.btnValidate.Name = "btnValidate";
			this.btnValidate.Size = new System.Drawing.Size(96, 24);
			this.btnValidate.TabIndex = 11;
			this.btnValidate.Text = "Validate &XML";
			this.btnValidate.Click += new System.EventHandler(this.btnValidate_Click);
			// 
			// tabPage2
			// 
			this.tabPage2.Controls.Add(this.btnClose);
			this.tabPage2.Controls.Add(this.btnStart);
			this.tabPage2.Controls.Add(this.rtb_EventLog);
			this.tabPage2.Location = new System.Drawing.Point(4, 22);
			this.tabPage2.Name = "tabPage2";
			this.tabPage2.Size = new System.Drawing.Size(592, 382);
			this.tabPage2.TabIndex = 1;
			this.tabPage2.Text = "Import";
			// 
			// btnClose
			// 
			this.btnClose.BackColor = System.Drawing.SystemColors.HotTrack;
			this.btnClose.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnClose.ForeColor = System.Drawing.SystemColors.Info;
			this.btnClose.Location = new System.Drawing.Point(8, 352);
			this.btnClose.Name = "btnClose";
			this.btnClose.Size = new System.Drawing.Size(96, 24);
			this.btnClose.TabIndex = 21;
			this.btnClose.Text = "&Close";
			this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
			// 
			// btnStart
			// 
			this.btnStart.BackColor = System.Drawing.SystemColors.HotTrack;
			this.btnStart.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnStart.ForeColor = System.Drawing.SystemColors.Menu;
			this.btnStart.Location = new System.Drawing.Point(480, 352);
			this.btnStart.Name = "btnStart";
			this.btnStart.Size = new System.Drawing.Size(96, 24);
			this.btnStart.TabIndex = 20;
			this.btnStart.Text = "&Start Import";
			this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
			// 
			// rtb_EventLog
			// 
			this.rtb_EventLog.Location = new System.Drawing.Point(12, 8);
			this.rtb_EventLog.Name = "rtb_EventLog";
			this.rtb_EventLog.Size = new System.Drawing.Size(568, 328);
			this.rtb_EventLog.TabIndex = 11;
			this.rtb_EventLog.Text = "";
			this.rtb_EventLog.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.rtb_EventLog_LinkClicked);
			// 
			// pictFooter
			// 
			this.pictFooter.Image = ((System.Drawing.Image)(resources.GetObject("pictFooter.Image")));
			this.pictFooter.Location = new System.Drawing.Point(0, 480);
			this.pictFooter.Name = "pictFooter";
			this.pictFooter.Size = new System.Drawing.Size(600, 64);
			this.pictFooter.TabIndex = 13;
			this.pictFooter.TabStop = false;
			// 
			// pictureBox1
			// 
			this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
			this.pictureBox1.Location = new System.Drawing.Point(0, 0);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(600, 72);
			this.pictureBox1.TabIndex = 12;
			this.pictureBox1.TabStop = false;
			// 
			// openStructDialog
			// 
			this.openStructDialog.DefaultExt = "xml";
			this.openStructDialog.FileName = "struct.xml";
			// 
			// chkTryToUpdateExistingItems
			// 
			this.chkTryToUpdateExistingItems.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.chkTryToUpdateExistingItems.Location = new System.Drawing.Point(16, 176);
			this.chkTryToUpdateExistingItems.Name = "chkTryToUpdateExistingItems";
			this.chkTryToUpdateExistingItems.Size = new System.Drawing.Size(320, 24);
			this.chkTryToUpdateExistingItems.TabIndex = 9;
			this.chkTryToUpdateExistingItems.Text = "Update existing li&st items";
			// 
			// ImportApplication
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(600, 542);
			this.Controls.Add(this.pictFooter);
			this.Controls.Add(this.pictureBox1);
			this.Controls.Add(this.Settings);
			this.Name = "ImportApplication";
			this.Text = "SharePoint Products and Technologies Import Wizard";
			this.Load += new System.EventHandler(this.SharePointImporter_Load);
			this.Settings.ResumeLayout(false);
			this.tabPage1.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			this.tabPage2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion
		[STAThread]
		static void Main(string[] args) 
		{
			AppDomain currentDomain = AppDomain.CurrentDomain;
			currentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledHandler);

			Application.Run(new ImportApplication());
			
		 }

		private void btnClose_Click(object sender, System.EventArgs e)
		{
			DialogResult result = MessageBox.Show("Are you sure you'd like to exit?", "Close Application", MessageBoxButtons.YesNo,MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,MessageBoxOptions.DefaultDesktopOnly);
			if(result == DialogResult.Yes)
			{
				try 
				{
					// Create an instance of StreamWriter to write to a file.
					using(StreamWriter sw = new StreamWriter("config.dat",false))
					{
						sw.WriteLine(txtSource.Text);
						sw.WriteLine(chkImportSecurity.Checked.ToString());
						sw.WriteLine(chkAllowImportIntoExistingSite.Checked.ToString());
						sw.WriteLine(chkAllowImportIntoExistingLibrary.Checked.ToString());
						sw.WriteLine(rdReportAll.Checked.ToString());
						sw.WriteLine(rdReportWarnings.Checked.ToString());
						sw.WriteLine(rdReportErrors.Checked.ToString());
						sw.WriteLine(txtTarget.Text);
						sw.WriteLine(chkImportDesign.Checked.ToString());
						sw.Flush();
					}
				}
				catch(IOException Ex)
				{
					Logger.Log(ErrorLevel.Information,"Could not write to config.dat. Error:"+Ex.Message );
				}
				this.Close();
			}
			

		
		}

		private void btnStart_Click(object sender, System.EventArgs e)
		{
			rtb_EventLog.Clear();
			if(null==ImportGlobals)
				ImportGlobals=new Importer();
			ErrorLevel level=ErrorLevel.Information;
			if(rdReportWarnings.Checked)
				level=ErrorLevel.Warning;
			else if (rdReportErrors.Checked)
				level=ErrorLevel.Error;
			Logger.Init("import" + DateTime.Now.ToString("MMddHHmm",CultureInfo.InvariantCulture) + ".log",level);
			Logger.LogDataSendingEvent += new Logger.LogDataEventHandler(IncomingEventLogData);
			if(ImportGlobals.Init())
				ImportGlobals.Process(txtSource.Text,txtTarget.Text,chkAllowImportIntoExistingSite.Checked,chkAllowImportIntoExistingLibrary.Checked,chkImportSecurity.Checked,File.Exists(txtSource.Text+"\\listtemplates.xml"),chkImportDesign.Checked,chkTryToUpdateExistingItems.Checked);
			Logger.LogDataSendingEvent -= new Logger.LogDataEventHandler(IncomingEventLogData);
			Logger.Close();
		}
		/// <summary>
		/// RichTextBox Event Logger component
		/// </summary>
		public void AddTextToLogRtf(string incomingLogData)
		{
			try
			{
				incomingLogData = DateTime.Now.ToLongTimeString()+ ":  " + incomingLogData + "\n";
				

				//NOTE: replace rtf_eventlog with your RTF window, or text window, in your window app, to display log info.
				rtb_EventLog.AppendText(incomingLogData);
				rtb_EventLog.Focus();
				int textLength = rtb_EventLog.TextLength;
				rtb_EventLog.SelectionStart = textLength -1;
				rtb_EventLog.SelectionLength = 0;
				rtb_EventLog.ScrollToCaret();
				this.Refresh();
			}
			catch(Exception ex)
			{
				Debug.WriteLine("error while trying to write events incoming to AddTextToLogRtf function, error:" + ex.Message + ", in location:" + ex.TargetSite);
			}
		}
		private void IncomingEventLogData(object sender, SPEventArgs e)
		{
			AddTextToLogRtf(e.Message);
		}

		private void btnValidate_Click(object sender, System.EventArgs e)
		{
			Settings.SelectedIndex = 1;
			rtb_EventLog.Clear();
			if(null==ImportGlobals)
				ImportGlobals=new Importer();
			ErrorLevel level=ErrorLevel.Information;
			if(rdReportWarnings.Checked)
				level=ErrorLevel.Warning;
			else if (rdReportErrors.Checked)
				level=ErrorLevel.Error;

			Logger.Init("XML import" + DateTime.Now.ToString("MMddHHmm",CultureInfo.InvariantCulture) + ".log",level);
			Logger.LogDataSendingEvent += new Logger.LogDataEventHandler(IncomingEventLogData);
			if(ImportGlobals.Init())
				ImportGlobals.ValidateFiles(txtSource.Text,File.Exists(txtSource.Text+"\\Listtemplates.xml"));
			Logger.LogDataSendingEvent -= new Logger.LogDataEventHandler(IncomingEventLogData);
			Logger.Close();
		

		}

		private void btnNext1_Click(object sender, System.EventArgs e)
		{
			Settings.SelectedIndex = 1;
		}

		private void btnFileOpen_Click(object sender, System.EventArgs e)
		{
			string startDir=Directory.GetCurrentDirectory();
			string directoryName=txtSource.Text;
			if(directoryName.IndexOf(".")>0)
				openStructDialog.InitialDirectory=directoryName.Substring(0,directoryName.LastIndexOf("\\"));
			else
				openStructDialog.InitialDirectory=txtSource.Text;
			if(openStructDialog.ShowDialog()== DialogResult.OK)
			{
				directoryName=openStructDialog.FileName;
				txtSource.Text=directoryName.Substring(0,directoryName.LastIndexOf("\\"));
			}
			Directory.SetCurrentDirectory(startDir);


		}

		private void SharePointImporter_Load(object sender, System.EventArgs e)
		{
			// Set up the delays for the ToolTip.
			toolTip1.AutoPopDelay = 5000;
			toolTip1.InitialDelay = 1000;
			toolTip1.ReshowDelay = 500;
			// Force the ToolTip text to be displayed whether or not the form is active.
			toolTip1.ShowAlways = true;
      
			
			toolTip1.SetToolTip(this.txtSource, "This is the directory containing the struct.xml file");
			toolTip1.SetToolTip(this.txtTarget,"This is the location where the content will be migrated");
			
			toolTip1.SetToolTip(this.chkImportSecurity,"This indicates if the importer will process the <security> nodes from the struct.xml file");
			toolTip1.SetToolTip(this.chkAllowImportIntoExistingSite,"This indicates whether import is allowed to an existing site");
			toolTip1.SetToolTip(this.chkAllowImportIntoExistingLibrary,"This indicates whether the import is allowed to an existing document library");
			toolTip1.SetToolTip(this.rdReportAll,"This reports everything");
			toolTip1.SetToolTip(this.rdReportWarnings,"This reports errors and warnings");
			toolTip1.SetToolTip(this.rdReportErrors,"This reports only the errors");

			
		}

		private void rtb_EventLog_LinkClicked(object sender, System.Windows.Forms.LinkClickedEventArgs e)
		{
			
			System.Diagnostics.Process.Start(e.LinkText);

		}



	}
}
